--[[
Author: wangxiaoyang
Date: 2020-10-10 10:06:20
LastEditTime: 2020-11-12 16:48:08
FilePath: \Lua\View\UIBase\UIComponent\UIComponentBase.lua

提供UI组件功能
本来应该在C#层去扩展，但是后期会使用FGUI
eg：扩展SrcollRect 增加近景远景功能
--]]

require 'Common/Class'
require 'View/UIBase/UIComponentInterface'
---@class UIComponentBase : UIComponentInterface
UIComponentBase = Class("UIComponentBase",UIComponentInterface)

local _M = UIComponentBase

function _M:ctor(obj,...)
    self.fguiObj = obj
    rawset(self,"_components",{})
    self:Create(...)
end

function _M:Create(...)
    self:OnComCreate(...)
end
function _M:OnComCreate(...)
    self.isEnable = false
    self:OnCreate(...)

end
---@deprecated 该方法已废弃， 使用定时器TimeCall
function _M:RegisterUpdate()
    rawset(self,"updateHandle",UpdateBeat:Add(self.Update,self))
    UpdateBeat:AddListener(self.updateHandle)
end

function _M:Enable()
    self:OnComEnable()
end

function _M:OnComEnable()
    self.isEnable = true
    self:OnEnable()
    if self._components then
        for key, value in pairs(self._components) do
            value:OnComEnable()
        end
    end
end

function _M:Disable()
    self:OnComDisable()
end

function _M:OnComDisable()
    self.isEnable = false
    self:OnDisable()
    if self._components then
        for key, value in pairs(self._components) do
            value:OnComDisable()
        end
    end
end

function _M:OnComDestroy()
    self.isEnable = false
    if self.updateHandle ~= nil then
        UpdateBeat:RemoveListener(self.updateHandle);
        self.updateHandle = nil
    end
    if self._components then
        for key, value in pairs(self._components) do
            value:OnComDestroy()
        end
    end
    self._components = nil
    self:OnDestroy()
    self:DelReferenceAll()
    self:UnRegisterEvent()
end

function _M:Destroy()
    self:OnComDestroy()
end
function _M:SetVisible(visible)
    if self.fguiObj then
        self.fguiObj.visible = visible
    end
end
--增加一个组件(luaCom继承UIComponentBase)
---@param obj GComponent
---@param luaCom UIComponentBase
function _M:CreateComponent(luaCom,obj,...)
    if self._components == nil then
        return
    end
    local tCom = luaCom.New(obj,...)
    tCom._parent = self
    table.insert(self._components,tCom)
    return tCom
end
-- 注册Event
function _M:RegisterEvent(key,onCall,handle)
    if self._eventList == nil then
        rawset(self,"_eventList",{})
    end
    local handle = handle or self
    local betterOnCall = function(...)
        if handle == nil then
            onCall(...)
        else
            onCall(handle,...)
        end
    end
    ListenEvent(key,betterOnCall)
    if self._eventList[key] then
        CloseEvent(key,self._eventList[key]);
    end
    self._eventList[key] = betterOnCall
end
-- 卸载Event
function _M:UnRegisterEvent()
    if self._eventList == nil then
        return
    end

    for key, value in pairs(self._eventList) do
        CloseEvent(key,value)
    end

    self._eventList = nil
end


function _M:Update()
    if not self.isEnable then
        return
    end
    self:OnUpdate()
end

function _M:OnCreate()
    -- 子类继承
end

function _M:OnEnable()
    -- 子类继承
end

function _M:OnDisable()
    -- 子类继承
end

function _M:OnDestroy()
    -- 子类继承
end

function _M:OnUpdate()
    -- 子类继承
end